﻿using iTextSharp.text.pdf;
using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace Tools
{
    public partial class GoogleCalendar : Base
    {
        Tools.Helper.Google google = new Tools.Helper.Google();
        public GoogleCalendar()
        {
            InitializeComponent();
            base.SetDefaultLocation();
            var obj = new Tools.Helper.Google();

            DataBindTime(DateTime.Now.ToString("HH:00"));
            GetLog(DateTime.Now);
        }

        string[] time = { "00:00", "00:30", "01:00", "01:30", "02:00", "02:30", "03:00", "03:30", "04:00", "04:30", "05:00", "05:30", "06:00", "06:30", "07:00", "07:30", "08:00", "08:30", "09:00", "09:30", "10:00", "10:30", "11:00", "11:30", "12:00", "12:30", "13:00", "13:30", "14:00", "14:30", "15:00", "15:30", "16:00", "16:30", "17:00", "17:30", "18:00", "18:30", "19:00", "19:30", "20:00", "20:30", "21:00", "21:30", "22:00", "22:30", "23:00", "23:30" };
        string[] time1 = { "00:00", "00:30", "01:00", "01:30", "02:00", "02:30", "03:00", "03:30", "04:00", "04:30", "05:00", "05:30", "06:00", "06:30", "07:00", "07:30", "08:00", "08:30", "09:00", "09:30", "10:00", "10:30", "11:00", "11:30", "12:00", "12:30", "13:00", "13:30", "14:00", "14:30", "15:00", "15:30", "16:00", "16:30", "17:00", "17:30", "18:00", "18:30", "19:00", "19:30", "20:00", "20:30", "21:00", "21:30", "22:00", "22:30", "23:00", "23:30" };
        /// <summary>
        /// 时间 格式 HH:00
        /// </summary>
        /// <param name="dateTimeStr"></param>
        public void DataBindTime(string dateTimeStr)
        {
            cob_start.DataSource = time;
            cob_start.SelectedItem = dateTimeStr;
        }

        private void btn_select_Click(object sender, EventArgs e)
        {
            GetLog(DateTime.Now);
        }

        private void btn_month_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("https://calendar.google.com/calendar/r/month");
        }

        private void btn_day_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("https://calendar.google.com/calendar/r/day");
        }

        private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
        {
            GetLog(this.monthCalendar1.SelectionEnd);
        }

        private void btn_add_Click(object sender, EventArgs e)//添加事件
        {
            if (chb_all.Checked)
            {
                var time = DateTime.Now.ToShortDateString();
                google.Add(txt_title.Text, "沈阳市, 中国辽宁省沈阳市", time, time);
            }
            else
            {
                var start = Picker_Start.Value.ToShortDateString() + " " + cob_start.Text;
                var end = Picker_End.Value.ToShortDateString() + " " + cob_end.Text;

                google.Add(txt_title.Text, "沈阳市, 中国辽宁省沈阳市", start, end);
            }
            GetLog(DateTime.Now);

            txt_title.Text = "";
            MessageBox.Show("添加事件成功!");
        }

        /// <summary>
        /// 获取日志
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        private void GetLog(DateTime time)
        {
            var timeStr = "";
            this.txt_log.Text = google.GetEvents(time, out timeStr);
            cob_start.SelectedItem = timeStr;
        }

        #region 批量添加

        private void button1_Click(object sender, EventArgs e)
        {
            //obj.Add("对客户端首页调整收尾:调整服务端颜色 和客户端区分开", "沈阳市, 中国辽宁省沈阳市", "2018-07-29", "2018-07-29");
            //获取文件夹下所有文件

            var list = CommonLib.FileHelper.GetFiles(@"C:\Users\shuyu\Desktop\日报\106.华盾\1");
            foreach (var item in list)
            {
                var name = System.IO.Path.GetFileName(item).Replace(".pdf", "");
                //var nr = CommonLib.FileHelper.FileRead(item,"gb2312");
                var nr = getPdf(item).Replace("\r\n", "\n");
                var arr = Regex.Split(nr, "\n", RegexOptions.IgnoreCase);
                var i = 1;
                foreach (var temp in arr)
                {
                    if (!string.IsNullOrEmpty(temp.Trim()))
                    {
                        var num = i.ToString();
                        if (i < 10)
                        {
                            num = "0" + i;
                        }
                        var _temp = num + "." + temp.Replace("2.下午", "2. 下午");
                        google.Add(_temp, "沈阳市, 中国辽宁省沈阳市", name, name);
                        i++;
                    }
                }
                txt_log.Text += name + "\r\n";
            }
            MessageBox.Show("完成");
        }

        private string getPdf(string pdffilename)
        {
            try
            {
                PdfReader pdfReader = new PdfReader(pdffilename);
                int numberOfPages = pdfReader.NumberOfPages;
                StringBuilder text = new StringBuilder();
                for (int i = 1; i <= numberOfPages; ++i)
                {
                    text.Append(iTextSharp.text.pdf.parser.PdfTextExtractor.GetTextFromPage(pdfReader, i));
                }
                pdfReader.Close();
                var nr = text.ToString();
                return nr;
            }
            catch (Exception ex)
            {
                var nr = "原因：" + ex.ToString();
                return "";
            }
        }
        #endregion

        private void cob_start_SelectedIndexChanged(object sender, EventArgs e)
        {
            cob_end.SelectedItem = Convert.ToDateTime(Picker_Start.Value.ToShortDateString() + " " + cob_start.Text).AddHours(1).ToString("HH:mm");

            var flag = false;
            cob_end.Items.Clear();

            foreach (var item in time1)
            {
                if (item == cob_start.Text)
                {
                    flag = true;
                }
                if (flag)
                {
                    cob_end.Items.Add(item);
                }
            }
            cob_end.SelectedItem = Convert.ToDateTime("2018-08-16 " + cob_start.Text).AddHours(1).ToString("HH:mm");
        }

        private void chb_tomorrow_CheckedChanged(object sender, EventArgs e)
        {
            if (chb_tomorrow.Checked)
            {
                txt_title.Text = "1.计划\r\n2.计划\r\n3.计划";
                cob_start.Text = "17:30";
                cob_end.Text = "17:30";
            }
            else
            {
                txt_log.Text = "";
            }
        }
    }
}
